<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Model\User;
// use App\Http\Requests\storeUserPostRequest;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
     $users = user::orderBy('id','desc')->paginate(5);
     return view('admin.showUserList')->with('users',$users);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //加载添加用户视图
        return view('admin.userAdd');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
    //验证规则
     $messages = [
        'email.required' => '邮箱必须填写',
        'name.required' => '用户名必须填写',
        'email.email' => '邮箱规则不合法',
        'phone.required' => '手机必须填写',
        'phone.digits' => '手机号码格式不正确',
        'password.required' => '密码必须填写',
        'password.confirmed' => '两次密码输入不一致',
        ];

        $this->validate($request, [
        'name' => 'required',
        'password' => 'required|confirmed',
        'email' => 'required|email',
        'phone' => 'required|digits:11',
    ],$messages);

        //执行添加操作
        $user = new user();
        $user->name = $request->name;
        $user->password = Hash::make($request->password);
        $user->email = $request->email;
        $user->phone = $request->phone;
        $user->nickName = $request->nickName;
        $user->save();
        return redirect('/user');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
